package com.har.modules.web.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.har.common.exception.RRException;
import com.har.common.utils.DateUtils;
import com.har.common.utils.MapUtils;
import com.har.common.utils.ValidatUtil;
import com.har.modules.web.dao.SmsDao;
import com.har.modules.web.entity.SmsEntity;
import com.har.modules.web.service.SmsService;
import org.springframework.stereotype.Service;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;


@Service("smsService")
public class SmsServiceImpl extends ServiceImpl<SmsDao, SmsEntity> implements SmsService {

    @Override
    public void save(String verificationCode,Long mobile) {

        SmsEntity smsEntity=new SmsEntity();
        //截取6位有效验证码
        String code=verificationCode.substring(11,17);
        smsEntity.setVerificationCode(code);
        Date day=new Date();
        //时间格式(yyyy-MM-dd HH:mm:ss)
        SimpleDateFormat df = new SimpleDateFormat(DateUtils.DATE_TIME_PATTERN);
        //时间格式(yyyy-MM-dd)
        SimpleDateFormat df1 = new SimpleDateFormat(DateUtils.DATE_PATTERN);
        //创建日期
        smsEntity.setCreateDate(DateUtils.stringToDate(df1.format(day),DateUtils.DATE_PATTERN));
        //创建时间
        smsEntity.setCreateTime(DateUtils.stringToDate(df.format(day),DateUtils.DATE_TIME_PATTERN));
        //设置验证码有效时间  10分钟
        smsEntity.setExpireTime(DateUtils.addDateMinutes(smsEntity.getCreateTime(),10));
        smsEntity.setMobile(mobile);
        this.insert(smsEntity);
    }

    @Override
    public void saveCommon(String data, Long mobile) {
        SmsEntity smsEntity=new SmsEntity();
        //截取6位有效验证码
        String code=data.substring(9,15);
        smsEntity.setVerificationCode(code);
        Date day=new Date();
        //时间格式(yyyy-MM-dd HH:mm:ss)
        SimpleDateFormat df = new SimpleDateFormat(DateUtils.DATE_TIME_PATTERN);
        //时间格式(yyyy-MM-dd)
        SimpleDateFormat df1 = new SimpleDateFormat(DateUtils.DATE_PATTERN);
        //创建日期
        smsEntity.setCreateDate(DateUtils.stringToDate(df1.format(day),DateUtils.DATE_PATTERN));
        //创建时间
        smsEntity.setCreateTime(DateUtils.stringToDate(df.format(day),DateUtils.DATE_TIME_PATTERN));
        //设置验证码有效时间  10分钟
        smsEntity.setExpireTime(DateUtils.addDateMinutes(smsEntity.getCreateTime(),10));
        //设置防刷 间隔时间
        MapUtils.map.put("spaceTime",DateUtils.addDateMinutes(smsEntity.getCreateTime(),2));

        smsEntity.setMobile(mobile);
        this.insert(smsEntity);

    }

    /**
     * 检验验证码是否失效
     * @param mobile
     * @param code
     * @return
     */
    public  Boolean contrastDate(String mobile,String code) throws ParseException {
        Boolean flag=false;
        //验证当前手机号是否正确
        if(!ValidatUtil.checkMobileNumber(mobile)){
            throw  new RRException("请输入正确的手机号");
        }
        SmsEntity smsEntity=this.selectOne(new EntityWrapper<SmsEntity>()
                .eq("mobile",mobile)
                .eq("verification_code",code));
        if(null==smsEntity){
            throw new RRException("手机号或验证码错误，请重新输入");
        }
        SimpleDateFormat df = new SimpleDateFormat(DateUtils.DATE_TIME_PATTERN);
        DateFormat dateFormat = new SimpleDateFormat(DateUtils.DATE_TIME_PATTERN);
        //当前时间
        Date  dateTime1 = dateFormat.parse(df.format(new Date()));
        //失效时间
        Date  dateTime2 = dateFormat.parse(df.format(smsEntity.getExpireTime()));
        //比较时间
        if(dateTime1.after(dateTime2)){
            flag=false;
        }else {
            flag=true;
        }
        return flag;
    }
}
